# .NET 9应用程序发布脚本
# 用于构建和发布应用程序到Windows Server 2019

param(
    [Parameter(Mandatory=$false)]
    [string]$Configuration = "Release",
    
    [Parameter(Mandatory=$false)]
    [string]$OutputPath = "./publish",
    
    [Parameter(Mandatory=$false)]
    [string]$Runtime = "win-x64",
    
    [Parameter(Mandatory=$false)]
    [switch]$SelfContained = $false,
    
    [Parameter(Mandatory=$false)]
    [switch]$SingleFile = $false,
    
    [Parameter(Mandatory=$false)]
    [switch]$Trimmed = $false
)

# 获取项目根目录
$projectRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $projectRoot

Write-Host "开始发布 .NET 9 应用程序..." -ForegroundColor Blue
Write-Host "配置: $Configuration" -ForegroundColor Cyan
Write-Host "输出路径: $OutputPath" -ForegroundColor Cyan
Write-Host "运行时: $Runtime" -ForegroundColor Cyan
Write-Host "自包含: $SelfContained" -ForegroundColor Cyan
Write-Host "单文件: $SingleFile" -ForegroundColor Cyan
Write-Host "裁剪: $Trimmed" -ForegroundColor Cyan
Write-Host ""

# 清理之前的发布输出
if (Test-Path $OutputPath) {
    Write-Host "清理之前的发布输出..." -ForegroundColor Yellow
    Remove-Item -Path $OutputPath -Recurse -Force
}

# 构建发布命令
$publishArgs = @(
    "publish"
    "--configuration", $Configuration
    "--output", $OutputPath
    "--runtime", $Runtime
    "--framework", "net9.0"
    "--verbosity", "minimal"
)

if ($SelfContained) {
    $publishArgs += "--self-contained", "true"
} else {
    $publishArgs += "--self-contained", "false"
}

if ($SingleFile) {
    $publishArgs += "--property:PublishSingleFile=true"
}

if ($Trimmed -and $SelfContained) {
    $publishArgs += "--property:PublishTrimmed=true"
}

# 添加优化选项
$publishArgs += "--property:DebugType=None"
$publishArgs += "--property:DebugSymbols=false"

try {
    Write-Host "执行发布命令..." -ForegroundColor Blue
    Write-Host "dotnet $($publishArgs -join ' ')" -ForegroundColor Gray
    Write-Host ""
    
    & dotnet @publishArgs
    
    if ($LASTEXITCODE -ne 0) {
        throw "发布失败，退出代码: $LASTEXITCODE"
    }
    
    Write-Host "" 
    Write-Host "发布成功!" -ForegroundColor Green
    
    # 显示发布信息
    $publishedFiles = Get-ChildItem -Path $OutputPath -Recurse -File
    $totalSize = ($publishedFiles | Measure-Object -Property Length -Sum).Sum
    $totalSizeMB = [math]::Round($totalSize / 1MB, 2)
    
    Write-Host "发布统计:" -ForegroundColor Yellow
    Write-Host "  文件数量: $($publishedFiles.Count)" -ForegroundColor White
    Write-Host "  总大小: $totalSizeMB MB" -ForegroundColor White
    Write-Host "  输出路径: $(Resolve-Path $OutputPath)" -ForegroundColor White
    
    # 检查主要文件
    $mainExecutable = Join-Path $OutputPath "Lzfy_His_Service.exe"
    if (Test-Path $mainExecutable) {
        $exeInfo = Get-Item $mainExecutable
        $exeSizeMB = [math]::Round($exeInfo.Length / 1MB, 2)
        Write-Host "  主程序: Lzfy_His_Service.exe ($exeSizeMB MB)" -ForegroundColor White
    }
    
    # 检查配置文件
    $configFiles = @("appsettings.json", "appsettings.Production.json")
    foreach ($configFile in $configFiles) {
        $configPath = Join-Path $OutputPath $configFile
        if (Test-Path $configPath) {
            Write-Host "  配置文件: $configFile ✓" -ForegroundColor Green
        } else {
            Write-Host "  配置文件: $configFile ✗" -ForegroundColor Red
        }
    }
    
    Write-Host ""
    Write-Host "部署建议:" -ForegroundColor Yellow
    Write-Host "1. 将发布文件复制到目标服务器" -ForegroundColor White
    Write-Host "2. 确保目标服务器已安装 .NET 9 Runtime (如果不是自包含部署)" -ForegroundColor White
    Write-Host "3. 配置数据库连接字符串" -ForegroundColor White
    Write-Host "4. 配置防火墙规则" -ForegroundColor White
    Write-Host "5. 使用 install-service.ps1 安装为Windows服务" -ForegroundColor White
    
    Write-Host ""
    Write-Host "安装服务示例命令:" -ForegroundColor Yellow
    $servicePath = Join-Path (Resolve-Path $OutputPath) "Lzfy_His_Service.exe"
    Write-Host "./install-service.ps1 -BinaryPath \"$servicePath\"" -ForegroundColor Cyan
    
} catch {
    Write-Error "发布过程中发生错误: $($_.Exception.Message)"
    exit 1
}

Write-Host "发布完成!" -ForegroundColor Green